package com.bioscript.standalone.imps;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bioscript.standalone.UtilMethods;
import com.bioscript.standalone.IO.ContigsFileParser;
import com.bioscript.standalone.IO.ContigsParsingEvents;
import com.bioscript.standalone.IO.ContigsParsingEvents.ContigParsingEventTypes;

public class ContigsBlastAlignment {
	
	public static void main(String[] args) throws FileNotFoundException
	{
		String contigRangeFile = args[0];
		String separateContigsFolder = args[1];
		separate(separateContigsFolder,contigRangeFile);
		
		String blastnScript = args[2];
		String tblastnScript = args[3];
		String database = args[5];
		String blastnOutFolder = args[6];
		String tblastnOutFolder = args[7];
		String blastnExe = args[8];
		String tblastnExe = args[9];
		make( blastnScript,  tblastnScript, 
				separateContigsFolder,  database,  blastnOutFolder, 
			 tblastnOutFolder,  blastnExe,  tblastnExe);
	}
	
	public static void make(String blastnScript, String tblastnScript, 
			String folderWithQueries, String database, String blastnOutFolder, 
			String tblastnOutFolder, String blastnExe, String tblastnExe) throws FileNotFoundException
	{
		File fl = new File(folderWithQueries);
		File[] names = fl.listFiles();
		PrintWriter pw = new PrintWriter(blastnScript);
		PrintWriter pw2 = new PrintWriter(tblastnScript);
		for(File name : names)
		{
			pw.println(blastnExe + " -query " + "./" + fl.getAbsolutePath() 
					+ " -db" + database + " -out " + blastnOutFolder + "/" + name.getName() + "_hits.txt");
			pw2.println(tblastnExe + " -query " + "./" + fl.getAbsolutePath() 
					+ " -db" + database + " -out " + tblastnOutFolder + "/" + name.getName() + "_hits.txt");
		}
		pw.flush();
		pw.close();
		pw2.flush();
		pw2.close();
	}
	
	private static void separate(String separateContigsFolder, String contigRangeFile)
	{
		List<String> contigNames = UtilMethods.readTabDelimitedFile(contigRangeFile, 0);
		Map<String,List<String>> map = new HashMap<String, List<String>>();
		ContigsFileParser<Map<String,List<String>>> contigsFileParser = 
			new ContigsFileParser<Map<String,List<String>>>(){
			private String currentContigName = null;
			public void handleEvent(ContigsParsingEvents event) 
			{
				if (event.getEventType() instanceof ContigParsingEventTypes)
				{
					switch((ContigParsingEventTypes) event.getEventType())
					{
					case contigStarted:
						currentContigName = (String)event.getSource();
						if (currentContigName.startsWith(">"))
							currentContigName = currentContigName.substring(1);
						currentContigName = currentContigName.split(" ")[0];
						List<String> lst = new ArrayList<String>(); 
						getValue().put(currentContigName, lst);
						break;
					case seqread:
						List<String> lt = getValue().get(currentContigName);
						lt.add((String)event.getSource());
						getValue().put(currentContigName, lt);
						break;
					}
				}
			}
		};
		contigsFileParser.setValue(map);
		for (String contig : contigNames)
		{
			List<String> lst = map.get(contig);
			if (lst != null && lst.size() > 0)
			{
				try
				{
					PrintWriter pw = new PrintWriter(separateContigsFolder + "/" + contig + ".fna");
					pw.println(">" + contig);
					for (String str : lst)
					{
						pw.println(str);
					}
					pw.flush();
					pw.close();
				}
				catch(Exception e)
				{
					e.printStackTrace();
				}
			}
		}
		map.clear();
	}
}
